//
//  XLNewHomeCell.m
//  XiangLove
//
//  Created by feng song on 2018/1/18.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLNewHomeCell.h"
#import "NSString+Ext.h"

@interface NewHomeImageView : UIImageView
@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@end
@implementation NewHomeImageView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.colors = @[(id)RGBA(0, 0, 0, 0).CGColor, (id)RGBA(0, 0, 0, 0.5).CGColor];
        _gradientLayer.locations = @[@(0.5)];
        _gradientLayer.startPoint = CGPointMake(0.5, 0);
        _gradientLayer.endPoint = CGPointMake(0.5, 1);
        [self.layer addSublayer:_gradientLayer];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _gradientLayer.frame = CGRectMake(0, self.height - 160, self.width, 160);
}
@end

@implementation XLNewHomeCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        [self addLoadViews];
    }
    return self;
}
-(void)addLoadViews
{
    _hostImgView                        = [[NewHomeImageView alloc] initWithFrame:CGRectMake(0, 0, (XQScreenW-27)/2, ((XQScreenW-27)/2)*200/174)];
    _hostImgView.userInteractionEnabled = YES;
    _hostImgView.clipsToBounds          = YES;
    _hostImgView.contentMode            = UIViewContentModeScaleAspectFill;
    _hostImgView.autoresizingMask       = UIViewAutoresizingFlexibleWidth;
    [_hostImgView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    ViewRadius(_hostImgView, 4);
    [self addSubview:_hostImgView];
    
   
    self.levelImage = [[UIImageView alloc]init];
//    _levelImage.backgroundColor = kWhiteColor;

    [self addSubview:_levelImage];
    
    _stateBgView            = [[UIView alloc] init];
    _stateBgView.backgroundColor     = RGBA_HEX(0x000000, 0.3);
    _stateBgView.layer.masksToBounds = YES;
    _stateBgView.layer.cornerRadius  = 3;
    _stateBgView.layer.borderColor   = kWhiteColor.CGColor;
//    _stateBgView.layer.borderWidth   = 0.5;
    [self addSubview:_stateBgView];
    [_stateBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hostImgView.mas_top).offset(7);
        make.right.equalTo(self.hostImgView.mas_right).offset(-11);
        make.size.mas_equalTo(CGSizeMake(40, 18));
    }];
    
    
    _hostState                   = [[UILabel alloc] init];
    _hostState.backgroundColor   = kClearColor;
    _hostState.textColor         = kWhiteColor;
    _hostState.font              = kSetFont(10);
    _hostState.textAlignment     = NSTextAlignmentCenter;
    [self addSubview:_hostState];
    [self.hostState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_stateBgView);
        make.centerX.equalTo(_stateBgView);
        make.size.mas_equalTo(CGSizeMake(40, 18));
    }];
    //在线状态的小圆点
    _onlineState                     = [[UIView alloc] init];
    _onlineState.layer.masksToBounds = YES;
    _onlineState.layer.cornerRadius  = 2.5;
    [_hostState  addSubview:_onlineState];
    [_onlineState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_hostState.mas_left).offset(5);
        make.centerY.equalTo(_hostState);
        make.size.mas_equalTo(CGSizeMake(5,5));
    }];
    
    
    _addrss                     = [[UILabel alloc] init];
    _addrss.backgroundColor     = kClearColor;
    _addrss.textColor           = RGBA_HEX(0xffffff, 0.6);
    _addrss.font                = kSetFont(12);
    _addrss.textAlignment       = NSTextAlignmentRight;
    [self addSubview:_addrss];
    [self.addrss mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.hostImgView.mas_bottom).offset(-8);
        make.right.equalTo(self.mas_right).offset(-6);
        
    }];
    _nickName                     = [[UILabel alloc] init];
    _nickName.textColor           = kWhiteColor;
    _nickName.font                = KSetBoldFont(14);
    [self addSubview:_nickName];
    [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.hostImgView.mas_bottom).offset(-6);
        make.left.equalTo(self.hostImgView.mas_left).offset(8);
        make.right.equalTo(_addrss.mas_left).offset(-5);
        
    }];
}
-(void)setDataModels:(NewCamgirlModel *)dataModels 
{
    if (_dataModels != dataModels) {
        _dataModels = dataModels;
    }

    self.nickName.text  = dataModels.NickName;_nickName.font = [UIFont systemFontOfSize:18];
    CGFloat width = [self.nickName.text sizeWidthsFont:15 andHeight:20];
    if (width > self.frame.size.width - 95) {
        width =  self.frame.size.width - 95;
    }
    _levelImage.frame = CGRectMake(width + 15 + 3, self.frame.size.height - 45, 26, 13);
    _levelImage.hidden = YES;
    [XLDownloadImageTool downloadImage:dataModels.CoverNewHeadImg place:[UIImage imageWithColor:RGB(255, 255, 255)] imageView:self.hostImgView];
    [self.levelImage setImageWithURL:[NSURL URLWithString:dataModels.LevelIcon] placeholder:nil];

    if ([dataModels.OnlineStatus isEqualToString:@"3"]){
        self.hostState.text = @"  在线";//tittle
        _stateBgView.hidden = NO;//底部灰色蒙层
        self.onlineState.backgroundColor = RGB_HEX(0x42C570);//小点
    }
    else if ([dataModels.OnlineStatus isEqualToString:@"2"]){
        self.hostState.text = @"  忙碌";
        _stateBgView.hidden = NO;
        self.onlineState.backgroundColor = kRedColor;
    }
    else{
        self.onlineState.backgroundColor = kClearColor;
        self.hostState.text = @"   ";
        _stateBgView.hidden = YES;
    }
    self.videChatBtn.hidden = YES;//永久隐藏
    self.addrss.text   = NSStringFormat(@"%.0f金豆/分钟",[dataModels.LiveChatPrice floatValue] * 10);
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        self.addrss.hidden = YES;
    }
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

@end
